package com.xy.base;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.xy.base.reciver.ExitReciver;
import com.xy.base.reciver.NetStateReciver;
import com.xy.manager.MsgManager;

/**
 * Created by John on 2016/10/26.
 */

public abstract class BaseActivity extends FragmentActivity {
    private MsgManager msgManager;
    private ExitReciver exitReciver = new ExitReciver();
    protected NetStateReciver netStateReciver = new NetStateReciver();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        msgManager = new MsgManager(this);
        registerReceiver(exitReciver,exitReciver.getIntentFilter());
        registerReceiver(netStateReciver,netStateReciver.getIntentFilter());
        initData();
        initView(savedInstanceState);
        loadData();
    }

    /**
     * 初始化数据
     */
    protected  void initData(){

    };
    /**
     * 加载网络数据
     */
    protected  void loadData(){

    };
    /**
     * 加载网络数据
     */
    protected abstract void initView(Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(exitReciver);
        if (netStateReciver!= null){
            unregisterReceiver(netStateReciver);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode){
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void showToastMsg(String str){
        msgManager.showToastMsg(str, Toast.LENGTH_SHORT);
    }

    public void showToastMsg(String str,int druation){
        msgManager.showToastMsg(str,druation);
    }

    public void showSnackMsg(String str){
        msgManager.showSnackMsg(str);
    }

    public void showSnackMsg(String str,int duration){
        msgManager.showSnackMsg(str, duration);
    }

    public void showLoading(boolean show){
      msgManager.showLoading(show);
    }
}
